package com.water.camera.mark;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.water.camera.bean.EditItem;
import com.water.camera.databinding.LayoutWatermark79Binding;
import com.water.camera.util.Utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class Mark79 extends BaseMark {

    LayoutWatermark79Binding binding;

    public Mark79(@NonNull Context context) {
        super(context);
    }

    public Mark79(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Mark79(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(int type) {
        if (type == 0) {
            binding = LayoutWatermark79Binding.inflate(LayoutInflater.from(getContext()));
            addView(binding.getRoot());
            hideView(binding.binding8, binding.binding10, binding.binding12);
        } else {
            binding = LayoutWatermark79Binding.inflate(LayoutInflater.from(getContext()), this, true);
        }
        update();
    }

    private void setTime() {
        long time = getTime();
        if (time <= 0) {
            return;
        }
        String s = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(time);
        String[] ss = s.split(" ");
        StringBuffer sb = new StringBuffer().append(ss[0].trim())
                .append(" ").append(Utils.dateToWeek(time, 0))
                .append(" ").append(ss[1].trim());
        binding.binding3.setText(sb.toString());
    }

    @Override
    public void update() {
        setText(binding.binding1, "title79", binding.binding1);
        setText(binding.binding3, "time79", binding.binding2);
        setTime();

        setText(binding.binding7, "didian79", binding.binding6);
        setText(binding.binding9, "jingwei79", binding.binding8);

        setText(binding.binding11, "operator79", binding.binding10);
        setText(binding.binding13, "beizhu79", binding.binding12);

        super.update();
    }

    @Override
    public ArrayList<EditItem> getEditItems() {
        ArrayList<EditItem> list = new ArrayList<>();
        list.add(new EditItem("title79", "标题"));
        list.add(new EditItem("time79", "时间", true));
        list.add(new EditItem("didian79", "地点"));
        list.add(new EditItem("jingwei79", "经纬度"));

        list.add(new EditItem("operator79", "操作员"));
        list.add(new EditItem("beizhu79", "备注"));

        return list;
    }

    @Override
    public void onValueChange(EditItem item) {
        if (item.isTime) {
            setTime();
        } else {
            super.onValueChange(item);
        }
    }
}
